package com.fenxiangtech.mall.controller;

import com.alibaba.fastjson.JSON;
import com.fenxiangtech.mall.base.BaseResponse;
import com.fenxiangtech.mall.base.PageData;
import com.fenxiangtech.mall.manager.CartManager;
import com.fenxiangtech.mall.model.Cart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author lixiang
 * @CreateTime 2018/10/27
 **/
@Controller
public class CartController extends AbstractBaseController {
    @Autowired
    private CartManager cartManager;



    @PostMapping("/cart/save")
    @ResponseBody
    public BaseResponse<String> saveItem(Cart cart){
        BaseResponse<String> baseResponse = new BaseResponse<>();
        cartManager.saveCart(cart);
        baseResponse.setData("OK");
        return  baseResponse;
    }

    @GetMapping("/cart/list")
    @ResponseBody
    public String listItem(Cart cart, Integer pageNumber , Integer pageSize){

        Map<String,Object> map  = new HashMap<>();

        List<Cart> carts = cartManager.queryCart(cart, pageNumber, pageSize);
        Long totalCount = cartManager.countCart(cart);
        map.put("total",totalCount);
        map.put("rows",carts);
        return JSON.toJSONString(map);
    }
    
    @GetMapping("/cart/get")
    @ResponseBody
    public Cart listItem(Cart cart){
    
        List<Cart> carts = cartManager.queryCart(cart, 1, 1);
        if(carts!=null && carts.size()>0){
            return carts.get(0);
        }else{
            return new Cart();
        }
    }

    @GetMapping("/cart/delete")
    @ResponseBody
    public String delete(Long id){
        cartManager.deleteCart(id);
        return "OK";
    }

}
